package com.yygo.dao.hibernate;

import com.yygo.dao.FeedbackDao;
import com.yygo.dao.GenericDao;
import com.yygo.model.Feedback;
import com.yygo.model.User;
import com.yygo.model.enums.FeedbackStatus;
import com.yygo.model.vo.FeedbackVO;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by tiansha on 2015/10/4.
 */
@Repository("feedbackDao")
public class FeedbackDaoHibernate extends GenericDaoHibernate<Feedback, Long> implements FeedbackDao {

    public FeedbackDaoHibernate() {
        super(Feedback.class);
    }

    @Override
    public List<Feedback> getFeedbacks(User currentUser) {
        Criteria criteria = getSession().createCriteria(Feedback.class);
        criteria.add(Restrictions.not(Restrictions.eq("feedbackStatus", FeedbackStatus.End)));
        criteria.add(Restrictions.or(
                Restrictions.eq("feedbackRequester", currentUser),
                Restrictions.eq("feedbackTarget", currentUser)
        ));
        criteria.addOrder(Order.desc("time"));
        List<Feedback> feedbacks = criteria.list();
        return feedbacks;
    }
}
